package com.funong.matrix.biz.massage.facade;

import com.alibaba.fastjson.JSON;
import com.funong.matrix.biz.base.entity.SysMsgTemplateEntity;
import com.funong.matrix.biz.massage.pojo.BusinessTemplateDTO;
import com.funong.matrix.biz.massage.pojo.MessageInfoDTO;
import com.funong.matrix.biz.massage.pojo.SendMessageDTO;
import com.github.pagehelper.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author houhaipeng
 * @version 1.0
 * @date 2021/3/18 17:28
 */
@Component
public class MessageBuildStrategyContext {

    private final Map<String, IMessageBuilder> strategyMap = new ConcurrentHashMap<>();

    @Autowired
    public void strategyInterface(Map<String, IMessageBuilder> strategyMapInfo) {
        this.strategyMap.clear();
        strategyMapInfo.forEach((k, v) -> this.strategyMap.put(k, v));
    }

    private IMessageBuilder getStrategy(String messageType) {
        return messageType != null ? strategyMap.get(messageType) : null;
    }

    //    public Map<String, Object> buildMessageContent(String messageType, Map<String, String> dataMap, DbmTemplateEntity templateEntity) {
//        return messageType != null ? this.getStrategy("b" + messageType).buildMessageContent(dataMap, templateEntity) : null;
//    }
    public Map<String, Object> buildMessageContent(String messageType, Map<String, Object> dataMap, BusinessTemplateDTO businessTemplateDTO) {
        if (messageType != null) {

            IMessageBuilder strategy = this.getStrategy("b" + messageType);
            SysMsgTemplateEntity templateEntity = businessTemplateDTO.getTemplateEntity();
            Map<String, Object> content = strategy.buildMessageContent(dataMap, templateEntity);
            return content;
        }
        return null;
    }

    //组装消息信息
    public SendMessageDTO buildMessageContent(String messageType, MessageInfoDTO messageInfoDTO, BusinessTemplateDTO businessTemplateDTO) {
        if (messageType != null) {

            IMessageBuilder strategy = this.getStrategy("b" + messageType);

            SysMsgTemplateEntity templateEntity = businessTemplateDTO.getTemplateEntity();

            SendMessageDTO sendMessageDTO = strategy.buildMessageContent(messageInfoDTO, templateEntity);

            String templateId = templateEntity.getId().toString();
            if (StringUtil.isNotEmpty(templateEntity.getThirdPartyId())) {
                templateId = templateEntity.getThirdPartyId();
            }

            sendMessageDTO.setMessageType(messageType)
                    .setBusinessCode(messageInfoDTO.getBusinessCode())
                    .setDataMap(messageInfoDTO.getDataMap())
                    .setOriginData(JSON.toJSONString(messageInfoDTO))
                    .setTemplateId(templateId)
                    .setReferBizId(messageInfoDTO.getReferBizId())
                    .setSystem(messageInfoDTO.getSystem())
                    .setReceivers(messageInfoDTO.getReceivers())
                    .setCopyers(messageInfoDTO.getCopyers())
                    .setFactoryCode(messageInfoDTO.getFactoryCode())
                    .setCategoryId(messageInfoDTO.getCategoryId())
                    .setSalesType(messageInfoDTO.getSalesType())
                    .setMenuCode(messageInfoDTO.getMenuCode())
                    .setBusinessScene(messageInfoDTO.getBusinessSceneCode())
            ;

            sendMessageDTO.setSender(businessTemplateDTO.getSender())
                    .setCopyer(businessTemplateDTO.getCopyer())
                    .setCopyerType(businessTemplateDTO.getCopyerType())
                    .setReceiverType(businessTemplateDTO.getReceiverType())
            ;

            return sendMessageDTO;
        }
        return null;
    }
}
